package com.acwing.chapter3.dui;

import java.io.*;

/**
 * @author anan
 * @create 2021-08-27 15:42
 */
public class Dui838 {
    private static int N = 100010;
    private static int[] h = new int[N];
    private static int size;
    private static int tmp;
    private static void down(int u){
        int t = u;
        if (2*u <= size && h[2*u] < h[u]) t = 2*u;
        if (2*u + 1 <= size && h[2*u + 1] < h[t]) t = 2*u + 1;
        if (t != u){
            tmp = h[u];
            h[u] = h[t];
            h[t] = tmp;
            down(t);
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        String[] str1 = reader.readLine().split(" ");
        int n = Integer.parseInt(str1[0]);
        int m = Integer.parseInt(str1[1]);
        size = n;
        String[] str2 = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) {
            h[i] = Integer.parseInt(str2[i - 1]);
        }

        for (int i = n/2; i > 0; i--){
            down(i);
        }

        while (m-- > 0){
            writer.write(h[1] + " ");
            h[1] = h[size--];
            down(1);
        }
        writer.flush();
        writer.close();
        reader.close();
    }
}
